package androidcamp.socialfitnesstracker;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import androidcamp.socialfitnesstracker.data.WorkoutType;

public class NewWorkout extends Activity {
    
    public static final int START_TIMER = 1;
    public static final String WORKOUT_TYPE = "WORKOUT_TYPE";

    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_workout);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
		Log.i("workoutMenuActivity ", "started");

//        Map<Button, String> buttonTypes = new HashMap<Button, String>();
//        buttonTypes.put((Button) findViewById(R.id.running), WORKOUT_TYPE_RUNNING);
//        buttonTypes.put((Button) findViewById(R.id.jogging), WORKOUT_TYPE_JOGGING);
//        buttonTypes.put((Button) findViewById(R.id.swimming), WORKOUT_TYPE_SWIMMING);
//        buttonTypes.put((Button) findViewById(R.id.yoga), WORKOUT_TYPE_YOGA);
//
//        for(Map.Entry<Button, String> entry: buttonTypes.entrySet()) {
//            entry.getKey().setOnClickListener(new View.OnClickListener() {
//                public void onClick(View view) {
//                    Intent theIntent = new Intent(NewWorkout.this, WorkoutActivityTimer.this);
//                }
//            });
//        }
        
        // open up running activity
        ((Button) findViewById(R.id.running)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent theIntent = new Intent(NewWorkout.this, DoingActivity.class);
                theIntent.putExtra(WORKOUT_TYPE, WorkoutType.RUNNING);
                startActivityForResult(theIntent, START_TIMER);
            }
        });

        // open up running activity
        ((Button) findViewById(R.id.jogging)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent theIntent = new Intent(NewWorkout.this, DoingActivity.class);
                theIntent.putExtra(WORKOUT_TYPE, WorkoutType.JOGGING);
                startActivityForResult(theIntent, START_TIMER);
            }
        });

        // open up running activity
        ((Button) findViewById(R.id.swimming)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent theIntent = new Intent(NewWorkout.this, DoingActivity.class);
                theIntent.putExtra(WORKOUT_TYPE, WorkoutType.SWIMMING);
                startActivityForResult(theIntent, START_TIMER);
            }
        });

        // open up running activity
        ((Button) findViewById(R.id.yoga)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent theIntent = new Intent(NewWorkout.this, DoingActivity.class);
                theIntent.putExtra(WORKOUT_TYPE, WorkoutType.YOGA);
                startActivityForResult(theIntent, START_TIMER);
            }
        });

	}

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(WorkoutActivityTimer.LOG_TAG, "requestCode: " + requestCode
                + "; resultCode: " + resultCode
                + "; data: " + data);
        switch (requestCode) {
            case START_TIMER:
                if(resultCode == RESULT_OK) {
                    long timeSpent = data.getExtras().getLong(DoingActivity.ACTIVITY_TIME_SPENT);
                    WorkoutType workoutType = (WorkoutType) data.getExtras().get(DoingActivity.WORKOUT_ACTIVITY_TYPE);
                    Log.d(WorkoutActivityTimer.LOG_TAG, "time spent: " + timeSpent + "; activity: " + workoutType);
                    setResult(RESULT_OK, data);
                    finish();
                }
                break;
            default:
                Log.e(WorkoutActivityTimer.LOG_TAG, "unknown result type!");
        }
    }

}
